package com.widgets;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.GradientDrawable;
import android.util.AttributeSet;

import androidx.appcompat.widget.AppCompatButton;

import com.my_view_library.R;

/**
 * 圆角按钮
 */

public class ShapeButton extends AppCompatButton {

    private int radius;

    private int strokeWidth;

    private int strokeColor;

    private int fillColor;

    public ShapeButton(Context context) {
        this(context,null);
    }

    public ShapeButton(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public ShapeButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.ShapeButton,0,0);

        try {
            radius = ta.getInteger(R.styleable.ShapeButton_radiuss,0);
            strokeWidth = ta.getInteger(R.styleable.ShapeButton_strokeWidths,0);
            strokeColor = ta.getColor(R.styleable.ShapeButton_strokeColor,0xffABABAB);
            fillColor = ta.getColor(R.styleable.ShapeButton_fillColor,0xffABABAB);

        }finally {
            ta.recycle();
        }

        init(context);
    }

    private void init(Context context) {
        GradientDrawable gradientDrawable = new GradientDrawable();

        if(radius != 0){
            gradientDrawable.setCornerRadius(15);
            if(strokeWidth != 0){
                gradientDrawable.setStroke(strokeWidth,strokeColor);
            }
            gradientDrawable.setColor(fillColor);

            setBackgroundDrawable(gradientDrawable);
        }
    }



    public void setFillColor(int color){
        GradientDrawable gradientDrawable = new GradientDrawable();
        if(radius != 0){
            gradientDrawable.setCornerRadius(15);
            if(strokeWidth != 0){
                gradientDrawable.setStroke(strokeWidth,strokeColor);
            }
            gradientDrawable.setColor(color);
            setBackgroundDrawable(gradientDrawable);
        }
    }

    public void setStrokeColor(int color){
        GradientDrawable gradientDrawable = new GradientDrawable();

        if(radius != 0){
            gradientDrawable.setCornerRadius(15);
            if(strokeWidth != 0){
                gradientDrawable.setStroke(strokeWidth,color);
            }
            gradientDrawable.setColor(fillColor);

            setBackgroundDrawable(gradientDrawable);
        }
    }
}
